<template>
  <div :id="randomId"> </div>
</template>

<script>
  import E from 'wangeditor'
  export default {
    name: 'Editor',
    props: {
      value: {
        default: function () {
          return ''
        }
      }
    },
    data() {
      return {
        randomId: 'editor_' + Math.random() * 100000000000000000,
        // 编辑器实例
        instance: null,
        ready: false
      }
    },
    watch: {
      value: function (val, oldVal) {
        if (val != null && this.ready) {
          this.instance = new E(`#${this.randomId}`)
          this.instance.create()
          this.instance.txt.html(val)
        }
      }
    },
    mounted() {
      this.initEditor()
    },

    beforeDestroy() {
      if (this.instance !== null && this.instance.destroy) {
        this.instance.destroy()
      }
    },
    methods: {
      initEditor() {
        this.$nextTick(() => {
          console.log(window)
          this.instance = new E(`#${this.randomId}`)
          this.instance.create()
          this.ready = true
          this.$emit('ready', this.instance)
        })
      },
      getUEContent() {
        return this.instance.getContent()
      },
      setText(con) {
        this.instance = new E(`#${this.randomId}`)
        this.instance.create()
        this.instance.txt.html(con)
      }
    }
  }
</script>
